#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.biz.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpClient {

	public static String get(String url) {
		InputStream input = null;

		try {
            URL _url = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            conn.connect();

            input = conn.getInputStream();

            ByteArrayOutputStream outdata = new ByteArrayOutputStream();
            int length = 0;
            byte[] buffer = new byte[4096];
            while ((length = input.read(buffer)) >= 0) {
                outdata.write(buffer, 0, length);
            }

            conn.disconnect();

            String result = new String(outdata.toByteArray(), "utf-8");
            return result;
        }catch(Exception e){

		} finally {
			try {
				input.close();
			} catch (Exception e) {
			}
		}
        return null;
	}

	public static String post(String url, String body)  {
		InputStream input = null;
		OutputStream output = null;

		try {
			URL _url = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
			conn.setDoOutput(true);
			conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			conn.connect();
			output = conn.getOutputStream();
			output.write(body.getBytes("utf-8"));
			output.flush();
 
			input = conn.getInputStream();

			ByteArrayOutputStream outdata = new ByteArrayOutputStream();
			int length = 0;
			byte[] buffer = new byte[4096];
			while ((length = input.read(buffer)) >= 0) {
				outdata.write(buffer, 0, length);
			}

			conn.disconnect();

			String result = new String(outdata.toByteArray(), "utf-8");
			return result;
        }catch(Exception e){
		} finally {
			try {
				input.close();
			} catch (Exception e) {
			}
			try {
				output.close();
			} catch (Exception e) {
			}
		}
        return null;
	}
}
